package com.supertange.miniapp.wepet.modules.wepet.utils;

import com.supertange.miniapp.wepet.common.utils.Assert;
import com.supertange.miniapp.wepet.common.utils.FileUploadUtil;
import com.supertange.miniapp.wepet.modules.wepet.entity.TopicAnswerEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.TopicAnawerVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TopicUtil {


    //上传文件的根路径
//    private static String uploadRootPath = "/home/ubuntu/tmpfzk/upload/";
//    private static String uploadRootPath = " E:/English/Project20181224/upload/";
//    private static String uploadRootPath = "/opt/suse/upload/";
    private static String uploadRootPath = "E:\\English\\Project20181224\\upload\\";


    public static File qnImageUpload(MultipartFile file,String topicId,int index) {
        //取文件扩展名
        if(file!=null) {
            File dir = new File(uploadRootPath+"topic/"+topicId);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String fileName = index + ".png";
            String newFilename = uploadRootPath + "topic/" + topicId+"/"+ fileName;
            File newFile = new File(newFilename);
            if (newFile.exists()) {
                newFile.delete();
            }
            try {
                file.transferTo(newFile);
                return newFile;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }else{
            return null;
        }
    }

    public static boolean qnImageDownload( String topicId,HttpServletRequest request,
                                           HttpServletResponse response) {
        String fileName = uploadRootPath+"topic/"+topicId+"1.jpg";// 文件名
        if (fileName != null) {
            //设置文件路径
            File file = new File(fileName);
            //File file = new File(realPath , fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + "img");// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return true;
    }

    public static int[] getRandom(int min, int max, int n){
        if (n > (max - min + 1) || max < min) {
            return null;
        }
        int[] result = new int[n];
        int count = 0;
        Random r = new Random();
        while(count < n) {
            int num = r.nextInt(max);
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if(num == result[j]){
                    flag = false;
                    break;
                }
            }
            if(flag){
                result[count] = num;
                count++;
            }
        }
        return result;
    }

}
